import Opt from '@/components/dt/info/Opt.js'
import productOpt from '@/biz/option/product.js'
import bzColumn from './bzColumn.js'
import bzjColumn from './bzjColumn.js'
import tdColumn from './tdColumn.js'
import yjColumn from './yjColumn.js'
import bzbzjColumn from './bzbzjColumn.js'
import bzyjColumn from './bzyjColumn.js'
import dt from '@dt/dt'

class Column extends Opt {

  constructor(type, info, onBlur, edit = false) {
    super()
    this.type = type
    this.info = info
    this.onBlur = onBlur
    this.edit = edit
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.opt1 = [
    {
      type: 'index',
      title: '序号',
      width: 70,
      fixed: 'left'
    },
    {
      field: 'roomName',
      title: '房源名称',
      width: 180,
      fixed: 'left',
      header: {
        filter: {
          title: '房源名称',
          key: 'roomName',
          placeholder: '请输入',
          input: {}
        }
      },
    },
    {
      field: 'commissionOrderStatus',
      width: 120,
      header: {
        filter: {
          title: '房源状态',
          key: 'commissionOrderStatus',
          placeholder: '请选择',
          select: {
            key: 'commissionOrderStatus'
          }
        }
      },
      cell: {
        value: (row) => dt.dictInfo().__value('commissionOrderStatus', row
          .commissionOrderStatus),
        text: {}
      }
    },
    {
      field: 'contractDate',
      width: 120,
      header: {
        filter: {
          title: '签约日期',
          key: 'contractDate',
          placeholder: '请选择',
          date: {
            type: 'daterange'
          }
        }
      },
    },
    {
      field: 'customerName',
      width: 120,
      header: {
        filter: {
          title: '客户姓名',
          key: 'customerName',
          placeholder: '请输入',
          input: {}
        }
      },
    },
    {
      field: 'productName',
      width: 130,
      header: {
        filter: productOpt.create({
          key: 'productName',
          title: '产品',
          hid: this.info.housesId,
          data: {
            to: (v) => {
              return { productName: v.label }
            }
          }
        })
      },
    },
    {
      field: 'counselorName',
      width: 100,
      header: {
        filter: {
          title: '签约人',
          key: 'counselorName',
          placeholder: '请输入',
          input: {}
        }
      },
    },
    {
      field: 'channelName',
      title: '渠道',
      width: 120,
      cell: {
        value: (item) => item.channelName == '是' ? `${item.channelName}(${item.channelSourceName})` : item.channelName,
        text: {}
      }
    },
    {
      field: 'isAssociates',
      width: 100,
      header: {
        filter: {
          title: '关系户',
          key: 'isAssociates',
          placeholder: '请选择',
          select: {
            options: [
              { key: '1', value: '是' },
              { key: '0', value: '否' },
            ],
          }
        }
      },
      cell: {
        value: (row) => row.isAssociates ? '是' : '否',
        text: {}
      }
    },
    {
      field: 'isInsteadOfWorkers',
      width: 120,
      header: {
        filter: {
          title: '联代分佣',
          key: 'isInsteadOfWorkers',
          placeholder: '请选择',
          select: {
            options: [
              { key: '1', value: '是' },
              { key: '0', value: '否' },
            ],
          }
        }
      },
      cell: {
        value: (row) => row.isInsteadOfWorkers ? `是(${row.workersRatio}%)` : '否',
        text: {}
      }
    },
    {
      field: 'area',
      title: '面积(m²)',
      width: 100,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      field: 'unitPrice',
      title: '签约单价(元)',
      width: 130,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      field: 'basicTotalPrice',
      title: '底总价(元)',
      width: 130,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      field: 'totalPrice',
      title: '签约总价(元)',
      width: 130,
      cell: {
        text: {
          type: 'number'
        }
      }
    }, ]
    this.opt2 = [
    {
      field: 'adjustAmount',
      title: '调整值(元)',
      width: 130,
      cell: (row) => {
        if (this.edit && this.type != 'SKIP_POINT' && this.type != 'PREMIUM') {
          return {
            edit: {
              input: {
                type: 'number',
                error: row.adjustAmountErr,
                blur: () => {
                  this.onBlur(row, 'adjustAmount')
                }
              }
            }
          }
        } else {
          return {
            text: {
              type: 'number'
            }
          }
        }
      }
    },
    {
      field: 'actualAgentCost',
      title: '本次可结代理费(元)',
      width: 160,
      cell: () => {
        return {
          value: (row) => row.actualAgentCost == 0 ? '0.00' : row.actualAgentCost,
          text: {
            type: 'number'
          }
        }
      }
    }]

    switch (this.type) {
      case 'STANDARD': { // 标准佣金
        this.typeOpt = bzColumn.columns(this.edit, this.onBlur);
        break;
      }
      case 'DEPOSIT': { // 保证金佣金
        this.typeOpt = bzjColumn.columns(this.edit, this.onBlur);
        break;
      }
      case 'SKIP_POINT': { // 跳点佣金
        this.typeOpt = tdColumn.columns();
        break;
      }
      case 'PREMIUM': { // 溢价佣金
        this.typeOpt = yjColumn.columns();
        break;
      }
      case 'STANDARD_AND_DEPOSIT': { // 标准+保证金佣金
        this.typeOpt = bzbzjColumn.columns(this.edit, this.onBlur);
        break;
      }
      case 'STANDARD_AND_PREMIUM': { // 标准+溢价佣金
        this.typeOpt = bzyjColumn.columns();
        break;
      }
      default: {
        this.typeOpt = bzColumn.columns(this.edit, this.onBlur);
        break;
      }
    }

    this.all = [
      ...this.opt1,
      ...this.typeOpt,
      ...this.opt2
    ]

    if (this.edit) {
      this.all.unshift({
        type: 'checkbox',
        width: 50,
        fixed: 'left'
      })
    }
  }
}

export default Column